package com.example.springbootv1.service.impl;

import com.example.springbootv1.mapper.PojoTestMapper;
import com.example.springbootv1.pojo.LoginLog;
import com.example.springbootv1.pojo.PojoTest;
import com.example.springbootv1.service.PojoTestService;
import com.example.springbootv1.util.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class PojoTestServiceImpl implements PojoTestService {
    @Autowired
    private PojoTestMapper pojoTestMapper;

    @Override
    public List<PojoTest> getList() {
        return pojoTestMapper.getList();
    }

    @Override
    public PojoTest queryById(Long id) {
        return pojoTestMapper.queryById(id);
    }

    @Override
    public int insert(PojoTest pojoTest) {
        int count = pojoTestMapper.insert(pojoTest);
        return count;
    }

    @Override
    public int update(PojoTest pojoTest) {
        return pojoTestMapper.update(pojoTest);
    }

    @Override
    public int deleteById(Long id) {
        return pojoTestMapper.deleteById(id);
    }

    @Override
    public PojoTest login(String name, String charType) {
        return pojoTestMapper.login(name, charType);
    }

    @Override
    public List<PojoTest> getObjByObj(PojoTest pojoTest) {
        return pojoTestMapper.getObjByObj(pojoTest);
    }

    @Override
    public List<PojoTest> getByFenYe(PojoTest pojoTest) {
        return pojoTestMapper.getByFenYe(pojoTest);
    }

    @Override
    public int deleteBatch(List<Long> ids) {
        return pojoTestMapper.deleteBatch(ids);
    }

    @Override
    public int getByFenYeCount(PojoTest pojoTest) {
        return pojoTestMapper.getByFenYeCount(pojoTest);
    }

    @Override
    public void exportJobLevelExcel(String[] colHeadName, List<Object[]> listObject, HttpServletResponse response) {
        InputStream input = this.getClass().getClassLoader().getResourceAsStream("excelmodel/demo.xls");
        HSSFWorkbook workbook = null;
        workbook = new ExcelUtil().outputExcelFile(listObject, input, colHeadName);// 创建一个excel文档
        if (workbook != null) {
            String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            OutputStream outputStream;
            try {
                outputStream = response.getOutputStream();
                workbook.write(outputStream);
                outputStream.flush();
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                response.getWriter().print("error");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
